/**
 * @Title: MainProjectCreateLogic.java
 * @Package cn.com.gome.generator.logic
 * @Description: 主项目创建
 * @author chenmin-ds
 * @date 2017年5月26日 下午2:49:05
 * @company cn.com.gome
 * @version V1.0
 */


package cn.com.gome.generator.logic;

import cn.com.gome.framework.dao.entity.TblChildProjectInfo;
import cn.com.gome.framework.dao.entity.TblProjectBasicInfo;
import com.gomeplus.frame.exception.LogicsException;
import com.gomeplus.frame.logic.ILogics;
import com.gomeplus.frame.logic.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author wangheming
 * @ClassName: DataSourceCreateLogic
 * @Description: 增加新的数据源属性文件
 * @date 2017年12月01日 下午3:58:05
 */
@Service
public class DataSourceCreateLogic implements ILogics<Map<String, Object>> {

    private Logger logger = LoggerFactory.getLogger("DataSourceCreateLogic");

    @Override
    public ResultEnum exec(Map map) throws LogicsException {
        try {
//            TblChildProjectInfo childProjectInfo = (TblChildProjectInfo) map.get("childProjectInfo");
            TblProjectBasicInfo projectBasicInfo = (TblProjectBasicInfo) map.get("projectBasicInfo");
            String realPath = (String)map.get("realPath");

            String pachagesPath = projectBasicInfo.getPackages().replace(".", "//");

            String propsPath = realPath+"";

            // 先读取 再添加 最后生成 覆盖
            File file = new File("");
            StringBuffer sb = new StringBuffer();
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.checkoutTimeout=5000\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.minPoolSize=10\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.maxPoolSize=50\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.maxIdleTime=7200\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.acquireIncrement=3\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.maxIdleTimeExcessConnections=3000\n");
            sb.append("\n");
            sb.append(projectBasicInfo.getProjectEng() + ".jdbc.driverClassName=" + projectBasicInfo.getDatabaseDriver() + "\n");
            sb.append(projectBasicInfo.getProjectEng() + ".jdbc.url=" + projectBasicInfo.getDatabaseUrl() + "\n");
            sb.append(projectBasicInfo.getProjectEng() + ".jdbc.username=" + projectBasicInfo.getDatabaseAccount() + "\n");
            sb.append(projectBasicInfo.getProjectEng() + ".jdbc.password=" + projectBasicInfo.getDatabasePassword() + "\n");
            file = new File(propsPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//dev//" + projectBasicInfo.getProjectEng() + "-jdbc.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(propsPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//dev//" + projectBasicInfo.getProjectEng() + "-jdbc.properties"), "UTF-8");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();


            return ResultEnum.OK;
        } catch (Exception e) {
            logger.error("创建子项目异常：", e);
        }
        return ResultEnum.PART_CASE_01;
    }
}
